package com.caht.servier.Impl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.caht.DTO.PostsDTO;
import com.caht.VO.PostsandUserVO;
import com.caht.context.BaseContext;
import com.caht.entity.Posts;
import com.caht.entity.likestate;
import com.caht.mapper.PostsMapper;
import com.caht.mapper.UserMapper;
import com.caht.mapper.likestateMapper;
import com.caht.servier.PostsServier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class PostsServierImpl implements PostsServier {

    @Autowired
    private PostsMapper postsMapper;
    @Autowired
    private likestateMapper likestateMapper;
    /**
     * 上传文章信息
     * @param posts
     * @return
     */
    @Override
    public void addposts(Posts posts) {
        //获取 上传文章者
        Long currentId = BaseContext.getCurrentId();
        //实现文章添加
        posts.setPostsUserid(String.valueOf(currentId));
        postsMapper.addposts(posts);
    }
    /**
     * 查询文章列表
     * @param
     * @return
     */
    @Override
    public List<PostsandUserVO> postsResult() {
        Long currentId = BaseContext.getCurrentId();
        //点赞列表
        List<likestate> likestateslist = likestateMapper.queryAll(currentId);
        //帖子列表
        List<PostsandUserVO> posts = postsMapper.ListPostsByID();
        //
        List<PostsandUserVO> PoatsLikesList = new ArrayList<>();
        for (PostsandUserVO post : posts) {
            for (likestate likestate : likestateslist) {
                if(likestate.getUserId() == currentId && likestate.getPoststId().equals(String.valueOf(post.getId()))) {
                    post.setLikeState(1L);
                    PoatsLikesList.add(post);
                    break;
                }
            }
        }
        return PoatsLikesList == null ? PoatsLikesList : posts;
    }

    /**
     * 实现点赞功能
     * @param likeState
     */
    @Override
    public void postsLikecount(Long likeState) {

    }
}
